package com.wangwenjun.concurrency.chapter14.concurrenthashmap;

import org.junit.Test;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/3/8 15:30
 * @Description:
 */
public class UseConcurrentHashmap {

    @Test
    public void test01() throws InterruptedException {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

        Thread t1 = new Thread(() -> {
            for (int i = 1; i <= 1000; i++) {
                map.merge("a", 1, (o,n) -> {
                    return o+1;
                });
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 1; i <= 1000; i++) {
                map.merge("a", 1, (o,n) -> {
                    return o+1;
                });
            }
        });

        Thread t3 = new Thread(() -> {
            for (int i = 1; i <= 1000; i++) {
                map.merge("a", 1, (o,n) -> {
                    return o+1;
                });
            }
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        System.out.println(map.get("a"));

    }

}
